<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      actionWidth="230"
    >
      <template #default>
        <el-table-column label="是否启用" align="center">
          <template #default="{ row }">
            {{ row.isEnable == 0 ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column label="启用时间" align="center" prop="enableDate">
        </el-table-column>
        <el-table-column label="审核人" align="center" prop="auditName">
        </el-table-column>
        <el-table-column label="审核意见" align="center" prop="auditOpinion">
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button type="text" icon="Edit" @click="handleQy(row)"
          >启用</el-button
        >
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['safetyRulesRegulations:update']"
          >查看</el-button
        >

        <!-- <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          >删除</el-button
        > -->
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
    ></DialogVform>
    <el-dialog append-to-body v-model="qyDialog" width="40%" title="启用">
      <template #default>
        <el-form
          label-width="100px"
          label-position="left"
          ref="refQyForm"
          :model="qiyongForm"
          :rules="rules"
        >
          <el-form-item label="是否启用">
            <el-select
              v-model="qiyongForm.isEnable"
              placeholder="Select"
              size="large"
            >
              <el-option label="是" :value="0" />
              <el-option label="否" :value="1" />
            </el-select>
          </el-form-item>
          <el-form-item label="启用日期">
            <el-date-picker
              v-model="qiyongForm.enableDate"
              type="date"
              placeholder=""
              :size="size"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="审核人">
            <el-input type="textarea" v-model="qiyongForm.auditName"></el-input>
          </el-form-item>
          <el-form-item label="审核意见">
            <el-input
              type="textarea"
              v-model="qiyongForm.auditOpinion"
            ></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="onQySubmit()"> 确 认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/aqsc/aqgz.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";

const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["docType", "docNumber", "docName"].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
const table = ref();

const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
} = useAsyncData({ APIS, formJson, dialogVform, table, proxy });
// 启用
const qiyongForm = ref({});
const shRow = ref({});
const qyDialog = ref(false);
const refQyForm = ref();
const rules = ref({
  firingDate: [
    {
      required: true,
      message: "字段不能为空",
    },
  ],
  enablePerson: [
    {
      required: true,
      message: "字段不能为空",
    },
  ],
  status: [
    {
      required: true,
      message: "字段不能为空",
    },
  ],
});
const close = () => {
  qyDialog.value = false;
};

const onQySubmit = () => {
  refQyForm.value.validate((valid, fields) => {
    if (valid) {
      APIS.update({
        ...shRow.value,
        ...qiyongForm.value,
      }).then(() => {
        qyDialog.value = false;
        getList();
      });
    } else {
    }
  });
};

const handleQy = (row) => {
  qiyongForm.value = {};
  qyDialog.value = true;
  shRow.value = row;
};
</script>

<style lang="scss" scoped></style>
